<?php

namespace App\Http\Controllers;

use App\Models\Cart;
use App\Models\Customer;
use App\Models\Product;
use App\Models\Store;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Whoops\Exception\ErrorException;

class CartController extends Controller
{
    /**
     * 列表
     * GET /api/cart
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $params = $request->all();
        if (empty($params['store_id'])) {
            return response()->json(['status' => 1009, 'message' => '门店ID不存在']);
        }

        $cart = new Cart();
        $list = $cart->getList($params);

        $data['total_amount'] = round(array_sum(array_column($list, 'total_price')) + array_sum(array_column($list, 'total_pack_price')), 2);
        $data['total_price'] = round(array_sum(array_column($list, 'total_price')), 2);
        $data['total_pack_price'] = round(array_sum(array_column($list, 'total_pack_price')), 2);
        $data['list'] = $list;

        return response()->json(['status' => 1, 'data' => $data]);
    }

    /**
     * 添加购物车
     * POST /api/cart/add
     * @param Request $request
     * @return JsonResponse
     */
    public function add(Request $request): JsonResponse
    {
        $params = $request->all();
        if (empty($params['customer_id'])) {
            return response()->json(['status' => 0, 'message' => '客户ID不存在']);
        }
        if (empty($params['store_id'])) {
            return response()->json(['status' => 0, 'message' => '门店ID不存在']);
        }
        if (empty($params['product_id'])) {
            return response()->json(['status' => 0, 'message' => '产品ID不存在']);
        }
        if (empty($params['count'])) {
            return response()->json(['status' => 0, 'message' => '加购数量不存在']);
        }

        $customer = Customer::where('id', $params['customer_id'])->first();
        if (empty($customer)) {
            return response()->json(['status' => 0, 'message' => '客户不存在']);
        }
        if ($customer->status != 'NORMAL') {
            return response()->json(['status' => 0, 'message' => '客户已禁用，无法下单']);
        }
        $product = Product::where('id', $params['product_id'])->first();
        if (empty($product)) {
            return response()->json(['status' => 0, 'message' => '加购产品不存在']);
        }
        if ($product->status != 'ENABLE') {
            return response()->json(['status' => 0, 'message' => '加购产品未上架']);
        }
        $store = Store::where('id', $params['store_id'])->first();
        if (empty($store)) {
            return response()->json(['status' => 0, 'message' => '加购门店不存在']);
        }
        if ($store->status != 'NORMAL') {
            return response()->json(['status' => 0, 'message' => '加购门店未营业']);
        }

        $cart = Cart::where('customer_id', $params['customer_id'])
            ->where('product_id', $params['customer_id'])
            ->where('store_id', $params['store_id'])
            ->first();

        if (!empty($cart) && empty($params['count'])) {
            Cart::where('customer_id', $params['customer_id'])
                ->where('product_id', $params['customer_id'])
                ->where('store_id', $params['store_id'])
                ->delete();
        }
        if (!empty($cart) && !empty($params['count'])) {
            Cart::where('customer_id', $params['customer_id'])
                ->where('product_id', $params['customer_id'])
                ->where('store_id', $params['store_id'])
                ->update(['count' => $params['count']]);
        }
        if (empty($cart)){
            Cart::create([
                'customer_id' => $params['customer_id'],
                'product_id' => $params['product_id'],
                'store_id' => $params['store_id'],
                'count' => $params['count'],
            ]);
        }

        return response()->json(['status' => 1, 'data' => []]);
    }

    /**
     * 清空购物策划
     * POST /api/cart/clear
     * @param Request $request
     * @return JsonResponse
     */
    public function clear(Request $request): JsonResponse
    {
        $params = $request->all();
        if (empty($params['customer_id'])) {
            return response()->json(['status' => 0, 'message' => '客户ID不存在']);
        }
        if (empty($params['store_id'])) {
            return response()->json(['status' => 0, 'message' => '门店ID不存在']);
        }

        Cart::where('customer_id', $params['customer_id'])
            ->where('store_id', $params['store_id'])
            ->delete();

        return response()->json(['status' => 1, 'data' => []]);
    }
}
